/**
 * unit tests for jquery.combobox
 * @author tjerkv
 * @since 02-11-2009
 * 
 * @todo use setup and teardown methods, see QUnit googlecode page for example
 */
module("jquery.ui.combobox");

//var combobox = $("#fixtureCombo").combobox();

test("class should be added to select", function() {
    $("#fixtureCombo").combobox();
    ok($("#fixtureCombo").hasClass("ui-combobox"));
});

test("select should have a button next to it", function() {
    $("#fixtureCombo").combobox();
    ok($("#fixtureCombo + #fixtureCombo-button-opendialog").length === 1);
});

test("a dialog should be present", function() {
    $("#fixtureCombo").combobox();
    ok($("#fixtureCombo-editor-dialog").length === 1);
});

test("dialog should be hidden on init", function() {
    $("#fixtureCombo").combobox();
    ok( !($("#fixtureCombo-editor-dialog").dialog("isOpen")) );
});

test("dialog should be displayed on button press", function() {
    $("#fixtureCombo").combobox();
    $("#fixtureCombo-button-opendialog").trigger("click");
    ok($("#fixtureCombo-editor-dialog").dialog("isOpen"));
    $("#fixtureCombo-editor-dialog").dialog("close");
});

test("empty new value should not close dialog", function() {
    $("#fixtureCombo").combobox();
    $("#fixtureCombo-button-opendialog").trigger("click");
    $("#fixtureCombo-newitem").val("");
    $("#fixtureCombo-button-save").trigger("click");
    ok($("#fixtureCombo-editor-dialog").dialog("isOpen"));
    $("#fixtureCombo-editor-dialog").dialog("close");
});

test("empty new value should trigger error event on save", function() {
    expect(1);
    $("#fixtureCombo").combobox();
    $("#fixtureCombo").bind("comboboxaddoptionerror", function(event, msg) {
        ok("event 'addoptionerror' was expected");
        //console.log(msg);
    });
    $("#fixtureCombo-button-opendialog").trigger("click");
    $("#fixtureCombo-newitem").val("");
    $("#fixtureCombo-button-save").trigger("click");    
    $("#fixtureCombo-editor-dialog").dialog("close");
});

test("good new value should close dialog and be added to select and should be selected", function() {
    expect(4);
    var initialNrOfOptions = $("#fixtureCombo option").length;
    $("#fixtureCombo").combobox();
    $("#fixtureCombo").bind("comboboxaddoption", function(event, msg) {
        ok("event 'addoption' was expected");        
    });
    $("#fixtureCombo-button-opendialog").trigger("click");
    $("#fixtureCombo-newitem").val("foobar");
    $("#fixtureCombo-button-save").trigger("click");
    ok( !($("#fixtureCombo-editor-dialog").dialog("isOpen")) );
    $("#fixtureCombo-editor-dialog").dialog("close");
    equals((initialNrOfOptions + 1), $("#fixtureCombo option").length);
    equals(3, $("#fixtureCombo").val());
});

test("two selects should be comboboxed in their own scope", function() {
    expect(6);
    $("#fixtureCombo").combobox();
    $("#fixtureCombo2").combobox();
    ok($("#fixtureCombo").hasClass("ui-combobox"));
    ok($("#fixtureCombo2").hasClass("ui-combobox"));
    ok($("#fixtureCombo-editor-dialog").length === 1);
    ok($("#fixtureCombo2-editor-dialog").length === 1);
    ok($("#fixtureCombo-button-opendialog").length === 1);
    ok($("#fixtureCombo2-button-opendialog").length === 1);
});

// @todo test overriding default options

